<?php echo $this->render('/public/header'); ?>
<?php $rules = $this->params['rules']; ?>
    <link href="/statics/css/css/bootstrap.min.css" rel="stylesheet"/>
    <link href="/statics/css/css/style.css" rel="stylesheet"/>
    <div id="main" class="col-xs-12">

        <select class="form-control" onchange="change_role(this)">
            <?php foreach ($this->params['rolelist'] as $k => $v) { ?>
                <option <?php if ($this->params['role'] == $v['id']) {
                    echo 'selected';
                }; ?> value="<?php echo $v['id']; ?>"><?php echo $v['role_name']; ?></option>
            <?php } ?>
        </select>

        <div class="tree well">
            <ul>
                <?php foreach ($this->params['list'] as $k => $v) {
                    ; ?>
                    <li class="parent_li">
                        <input value="<?php echo $v['id']; ?>" onclick="toggle_it(this)" type="checkbox"
                               name="rule" <?php if (in_array($v['id'], $rules)) {
                            echo "checked";
                        }; ?>/><span title="Collapse this branch"><i
                                    class="icon-folder-open"></i><?php echo $v['name']; ?></span>
                        <ul>
                            <?php if ($v['children']) { ?>
                                <?php foreach ($v['children'] as $k2 => $v2) { ?>
                                    <li class="parent_li">
                                        <input onclick="toggle_it(this)"
                                               value="<?php echo $v2['id']; ?>" <?php if (in_array($v2['id'], $rules)) {
                                            echo "checked";
                                        }; ?> type="checkbox" name="rule"/><span title="Collapse this branch"><i
                                                    class="icon-folder-open"></i><?php echo $v2['name']; ?></span>
                                        <?php if (!empty($v2['children'])) {
                                            foreach ($v2['children'] as $k3 => $v3) { ?>
                                                <ul>
                                                    <li class="parent_li">
                                                        <input onclick="toggle_it(this)"
                                                               value="<?php echo $v3['id']; ?>" <?php if (in_array($v3['id'], $rules)) {
                                                            echo "checked";
                                                        }; ?> type="checkbox" name="rule"/><span
                                                                title="Collapse this branch"><i
                                                                    class="icon-folder-open"></i><?php echo $v3['name']; ?></span>
                                                    </li>
                                                </ul>
                                            <?php }
                                        } ?>
                                    </li>
                                <?php } ?>
                            <?php } ?>
                        </ul>
                    </li>
                <?php } ?>
            </ul>
            <button class="btn btn-success" onclick="update_role_rules()">更新</button>
        </div>
    </div>
    <script>
        function update_role_rules() {
            var rules = [];
            if (layer.confirm('确认更新', {}, function () {
                    $("input[name=rule]:checked").each(function () {
                        rules.push(this.value);
                    })
                    $.post('/setting/update_role_rules', {
                        rules: rules,
                        role:<?php echo yii::$app->request->get('role') ? yii::$app->request->get('role') : 1;?>}, function (res) {
                        if (res.code == 1) {
                            layer.confirm('更新成功', {}, function () {
                                window.location.reload();
                            })
                        }
                        else {
                            layer.alert(res.msg);
                        }
                    }, 'json')
                })) ;
        }

        function change_role(e) {
            window.location.href = "?role=" + e.value;
        }
    </script>
    <script>
        function toggle_it(v) {
            var status = $(v)[0].checked;
            if (!status) {
                $(v).parent().find('input').each(function () {
                    $(this)[0].checked = false;
                })
            }
            else {
                $(v).parent().find('input').each(function () {
                    $(this)[0].checked = true;
                })
            }

        }

        $(function () {

            $('.tree li:has(ul)').addClass('parent_li').find(' > span').attr('title', 'Collapse this branch');

            $('.tree li.parent_li > span').on('click', function (e) {

                var children = $(this).parent('li.parent_li').find(' > ul > li');

                if (children.is(":visible")) {

                    children.hide('fast');

                    $(this).attr('title', 'Expand this branch').find(' > i').addClass('icon-plus-sign').removeClass('icon-minus-sign');

                } else {

                    children.show('fast');

                    $(this).attr('title', 'Collapse this branch').find(' > i').addClass('icon-minus-sign').removeClass('icon-plus-sign');

                }

                e.stopPropagation();

            });

        });

    </script>

<?php echo $this->render('/public/footer'); ?>